package com.xx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.entity.Employees;
import com.xx.entity.PageResult;
import com.xx.service.IEmployeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工登录表 前端控制器
 * </p>
 *
 * @author myx
 * @since 2024-12-09
 */
@RestController
@RequestMapping("/employees")
@Tag(name = "员工前端控制器",description = "员工接口")
public class EmployeesController {
    @Autowired
    IEmployeesService iEmployeesService;

    //添加员工
    @PostMapping
    public boolean save (@RequestBody Employees employees){
        return iEmployeesService.save(employees);
    }

    //通过ID删除员工
    @DeleteMapping("{id}")
    public boolean delete(@PathVariable Integer employeeId){
        return iEmployeesService.removeById(employeeId);
    }

    //修改
    @PutMapping("{id}")
    public boolean update(@PathVariable Integer employeeId,@RequestBody Employees employees){
        employees.setEmployeeId(employeeId);
        return iEmployeesService.updateById(employees);
    }

    //通过ID查询员工信息
    @GetMapping("{id}")
    public Employees getById(@PathVariable Integer employeeId){
        return iEmployeesService.getById(employeeId);
    }

    //分页查询
//    @GetMapping("page")
//    @Operation(summary = "员工分页接口",description = "获取分页数据")
//    @ApiResponses(value = {
//            @ApiResponse(responseCode = "200",description = "请求成功"),
//            @ApiResponse(responseCode = "201",description = "请求失败")
//    })
//    public PageResult<Employees> page(
//            @RequestParam(defaultValue = "1",name = "current",required = true) Integer current,
//            @RequestParam(defaultValue = "3",name = "current",required = true) Integer size) {
//        Page page = new Page<>(current, size);
////        return iEmployeesService;
//    }
    // 获取分页数据
    @GetMapping("page")
    @Operation(summary = "员工分页数据", description ="员工分页数据接口描述")
    @ApiResponses( value = {
            @ApiResponse(responseCode = "200", description = "请求成功"),
            @ApiResponse(responseCode = "201", description = "请求失败")
    }

    )
    public PageResult<Employees> getEmployeePage(
            @Parameter(name = "current", description = "当前页码", required = true) @RequestParam int current,
            @Parameter(name = "size", description = "每页显示条数", required = true)
            @RequestParam(value = "size", defaultValue = "5") // 默认每页显示5条数据
            int size) {
        Page<Employees> page = new Page<>(current, size);
        Page<Employees> resultPage = iEmployeesService.page(page);
        return PageResult.of(resultPage);
    }
}
